﻿using AutoMapper;
using App.Core.Attributes;

namespace App.Core.Extensions
{
    public static class MapperConfigurationExtension
    {
        public static void AddMapperConfiguration(IMapperConfigurationExpression configuration)
        {
            //有可能是其他程序集的映射，所以这里需要遍历所有程序集,找出所有纯在AutoMapperSign标签的映射
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (type.IsClass && type.IsDefined(typeof(AutoMapperSignAttribute), false))
                    {
                        //通过反射机制调用CreateMappings方法，创建映射
                        var method = type.GetMethod("CreateMappings");
                        if (method == null) continue;
                        method.Invoke(null, new object[] { configuration });
                    }
                }
            }
        }
    }
}
